package com.csg.ses.service.ses.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csg.ses.common.utils.CommonUtil;
import com.csg.ses.common.utils.MybatisPlusUtil;
import com.csg.ses.common.utils.SnowFlakeUtil;
import com.csg.ses.dao.ses.SysParamsDao;
import com.csg.ses.entity.ses.SysParamsEntity;
import com.csg.ses.service.ses.SysParamsService;
import com.csg.ses.vo.common.DropData;
import com.csg.ses.vo.common.IdListVo;
import com.csg.ses.vo.common.Result;
import com.csg.ses.vo.common.ResultStatus;
import com.csg.ses.vo.login.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统参数
 * @author wangjun
 * @date 2020/11/16
 */
@Service
@RequiredArgsConstructor
public class SysParamsServiceImpl extends ServiceImpl<SysParamsDao, SysParamsEntity> implements SysParamsService {
    private final SysParamsDao sysParamsDao;

    @Override
    public Result retrieve(SysParamsEntity sysParams, Long current, Long size) {
        try {
            Page<SysParamsEntity> page = MybatisPlusUtil.initPage(current, size, true);
            QueryWrapper<SysParamsEntity> qw = new QueryWrapper();
            if (!CommonUtil.isEmpty(sysParams.getParamTypeCode())) {
                qw.eq("param_type_code", sysParams.getParamTypeCode());
            }
            if (!CommonUtil.isEmpty(sysParams.getParamCode())) {
                qw.like("param_code", sysParams.getParamCode());
            }
            if (!CommonUtil.isEmpty(sysParams.getParamValue())) {
                qw.like("param_value", sysParams.getParamValue());
            }
            if (!CommonUtil.isEmpty(sysParams.getUseFlag())) {
                qw.eq("use_flag", sysParams.getUseFlag());
            }
            qw.orderByAsc("order_code");
            return new Result(sysParamsDao.retrieve(page, qw), ResultStatus.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    public Result getDropData(String paramType) {
        try {
            QueryWrapper<SysParamsEntity> qw = new QueryWrapper();
            qw.eq("param_type_code", paramType);
            qw.eq("use_flag", "Y");
            qw.orderByAsc("order_code");
            List<SysParamsEntity> list = sysParamsDao.selectList(qw);

            List<DropData> dropList = new ArrayList<DropData>();
            for (SysParamsEntity param : list) {
                DropData drop = new DropData();
                drop.setValue(param.getParamCode());
                drop.setLabel(param.getParamValue());
                dropList.add(drop);
            }
            return new Result(dropList, ResultStatus.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    @Transactional
    public Result del(IdListVo idList) {
        try {
            if (idList != null && idList.getIdList().size() > 0) {
                int num = sysParamsDao.deleteBatchIds(idList.getIdList());
                return new Result(num + "条记录删除成功！", ResultStatus.SUCCESS);
            } else {
                return new Result("未指定需要删除的记录！", ResultStatus.BIZ_FAILED);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    public Result getById(String id) {
        if (!CommonUtil.isEmpty(id)) {
            return new Result(sysParamsDao.selectById(id), ResultStatus.SUCCESS);
        } else {
            throw new RuntimeException("id不能为空！");
        }
    }

    @Override
    @Transactional
    public boolean save(SysParamsEntity sysParams) {
        try {
            if (!CommonUtil.isEmpty(sysParams.getParamTypeCode())) {
                sysParams.setModifiedBy(UserContext.getUserView().getLoginCode());
                sysParams.setModifyTime(new Timestamp(System.currentTimeMillis()));
                if (!CommonUtil.isEmpty(sysParams.getId())) {
                    sysParamsDao.updateById(sysParams);
                } else {
                    sysParams.setId(SnowFlakeUtil.nextStringId());
                    sysParams.setUseFlag(CommonUtil.isEmpty(sysParams.getUseFlag()) ? "Y" : sysParams.getUseFlag());
                    sysParams.setCreatedBy(UserContext.getUserView().getLoginCode());
                    sysParams.setCreateTime(new Timestamp(System.currentTimeMillis()));
                    sysParamsDao.insert(sysParams);
                }
                return true;
            } else {
                throw new RuntimeException("参数类型不能为空！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
}